package cn.com.nes.site.service.crm.customer;

import cn.com.nes.common.exception.MissRequestParameterException;
import cn.com.nes.common.util.id.IdUtils;
import cn.com.nes.mybatis.agent.crm.entity.Customer;
import cn.com.nes.site.entity.em.crm.customer.CustomerNatureEnum;
import cn.com.nes.site.entity.em.crm.customer.CustomerSaveTypeEnum;
import cn.com.nes.site.service.crm.customer.bo.CustomerBO;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.net.URLDecoder;

@Slf4j
@Data
public abstract class CustomerBuilder {

    public abstract CustomerNatureEnum getType();

    public Customer buildCustomer(CustomerBO bo) throws Exception {
        Customer customer = new Customer();
        validCommonFiled(bo);
        validDefaultField(bo);
        // 设置通用属性
        settingCommonFiled(bo, customer);
        // 设置默认属性
        settingDefaultField(bo, customer);
        return customer;
    }

    /**
     * 默认属性参数校验
     */
    protected abstract void validDefaultField(CustomerBO bo);

    /**
     * 设置默认属性
     */
    protected abstract void settingDefaultField(CustomerBO bo, Customer customer);

    /**
     * 通用属性参数校验
     */
    private void validCommonFiled(CustomerBO bo) {
        if (ObjectUtil.isEmpty(bo.getCustomerNature())){
            throw new MissRequestParameterException("客户性质");
        }
        // if (StrUtil.isEmpty(bo.getCustomerName())){
        //     throw new MissRequestParameterException("客户名称");
        // }
        // if (ObjectUtil.isEmpty(bo.getCustomerLevel())){
        //     throw new MissRequestParameterException("客户级别");
        // }
        // if (StrUtil.isEmpty(bo.getXzqhdm())){
        //     throw new MissRequestParameterException("行政区划");
        // }
        // if (StrUtil.isEmpty(bo.getAddress())){
        //     throw new MissRequestParameterException("客户地址");
        // }
        // if (StrUtil.isEmpty(bo.getPhone())){
        //     throw new MissRequestParameterException("手机号");
        // }
    }

    /**
     * 设置通用属性
     */
    private void settingCommonFiled(CustomerBO bo, Customer customer) throws Exception {
        if (bo.addCustomer()) {
            customer.setUuid(IdUtils.uuid());
            customer.setCreateUid(bo.getOperateUserId().toString());
            customer.setCreateTime(bo.getOperateDate());
            log.debug("CustomerBuilder settingCommonFiled 新增客户...");
        } else {
            customer.setUuid(bo.getUuid());
            log.debug("CustomerBuilder settingCommonFiled 编辑客户...");
        }
        customer.setCustomerNature(bo.getCustomerNature().getKey());
        customer.setCustomerLevel(bo.getCustomerLevel().getType());
        customer.setBusinessType(bo.getBusinessType());
        customer.setCustomerName(bo.getCustomerName());
        customer.setSex(bo.getSex());
        customer.setPhone(bo.getPhone());
        customer.setXzqhdm(bo.getXzqhdm());
        customer.setAddress(bo.getAddress());
        customer.setPersonInCharge(bo.getContact());
        customer.setCustomerSource(bo.getCustomerSource());
        customer.setMemo(bo.getMemo());
        customer.setLongitude(bo.getLongitude());
        customer.setLatitude(bo.getLatitude());
        customer.setSuperCustomerId(bo.getSuperCustomerId());
        customer.setPlanYear(bo.getPlanYear());
        customer.setIsHouseholder(bo.getHouseNumber());
        // customer.setElectricityMeterNumbers(bo.getElectricityMeterNumber());
        // customer.setGasMeterNumbers(bo.getGasMeterNumber());
        customer.setIdCardNumber(bo.getIdCardNumber());
        customer.setCustomerCode(bo.getCustomerCode());
        customer.setEnergyUsePropertyCode(bo.getEnergyUsePropertyCode());
        customer.setVillagers(bo.getIsVillagers());
        customer.setCompanyId(bo.getOperateUserCompanyId());
        customer.setPersonInCharge(bo.getContact());
        customer.setModifyUid(bo.getOperateUserId().toString());
        customer.setModifyTime(bo.getOperateDate());
        // saveType 等于 1（我的客户） 时，当前登录人为客户负责人
        if (ObjectUtil.isNotNull(bo.getSaveType()) && CustomerSaveTypeEnum._my == bo.getSaveType()) {
            customer.setCustomerOwnerId(bo.getOperateUserId());
        }
        if (StrUtil.isNotEmpty(bo.getJsonData())) {
            customer.setJsonData(URLDecoder.decode(bo.getJsonData(), "UTF-8"));
        }
    }
}
